package br.com.aratuk.util;

import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author ronieresousa
 */
public class Mensagem {

    public static final String SuccessFull = "msg.successFull";
    public static final String Failure = "msg.failure";
    public static final String Warn = "msg.warn";
    public static final String PermissaoNegada = "msg.permissaoNegada";

    public static String getMsgResource(FacesContext fc, String msg) {
        ResourceBundle rb = ResourceBundle.getBundle("br.com.aratuk.idioma.language", fc.getViewRoot().getLocale());
        return rb.getString(msg);
    }

    public static void messagemInfo(String... message) {

        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (message != null && message[0] != null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message[0], ""));
            }
        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, getMsgResource(context, Mensagem.SuccessFull), ""));
        }
    }

    public static void messagemError(String... message) {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (message[0] != null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message[0], ""));
            }
        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, getMsgResource(context, Mensagem.Failure), ""));
        }
    }

    public static void messagemWarn(String... message) {
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            if (message != null && message[0] != null) {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message[0], ""));
            }
        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, getMsgResource(context, Mensagem.Warn), ""));
        }
    }

    public static void messagemInfoRedirect(String message, String url) {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        FacesContext fc = FacesContext.getCurrentInstance();
        FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_INFO, getMsgResource(fc, message), ""));
        fc.getExternalContext().getFlash().setKeepMessages(true);
        try {
            context.redirect(url);
        } catch (IOException ex) {
            Logger.getLogger(Mensagem.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void redirect(String url) {
        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
        try {
            context.redirect(url);
        } catch (IOException ex) {
            Logger.getLogger(Mensagem.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
